package demo01Array;

import org.junit.Test;

/**
 * @ClassName $ {NAME}
 * @Description 数组中出现次数超过一般次数的数字
 * @Author 沈新源
 * @Date $ {DATE} $ {TIME}
 * @Version 1. 0
 **/

public class NumberOverHalf {
    int []arr ={1,2,3,2,2,2,5,4,2};


    public int MoreThanHalfNum_Solution(int [] array) {
        int result=array[0];
        int weight=1;
        for(int i =1;i<array.length;i++){
            if(array[i]==result){
                weight++;
            }else if(weight>0){
                weight--;
            }else if(weight==0){
                result=array[i];
                weight++;
            }
        }

        return result;
    }

    @Test
    public void Test(){
        System.out.println("MoreThanHalfNum_Solution(arr) = " + MoreThanHalfNum_Solution(arr));
    }
}
